/*
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is ST_EXT-S_UIMAST.
 *
 * The Initial Developer of the Original Code is
 * University of Rome, Tor Vergata
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 */

package it.uniroma2.art.uimast.impl.types;

import it.uniroma2.art.ontapi.ARTResource;
import it.uniroma2.art.ontapi.vocabulary.XmlSchema;
import it.uniroma2.art.uimast.xpts.IVariantVisitor;
import it.uniroma2.art.uimast.xpts.Variant;

import java.util.HashSet;
import org.apache.uima.resource.metadata.AllowedValue;

/**
 * A variant able to hold enumerated values.
 * 
 * @author <a href="mailto:manuel.fiorelli@gmail.com">Manuel Fiorelli</a>
 * 
 */
public class EnumeratedVariant extends Variant
{
	/**
	 * Constructs an <code>EnumeratedVariant</code>.
	 * @param allowedValues the allowed values
	 */
    public EnumeratedVariant(AllowedValue allowedValues[])
    {
    	//// Stores the allowed values
        this.allowedValues = new HashSet<String>();
        
        // Extracts the allowed strings
        for(int i = 0; i < allowedValues.length; i++)
        {
            AllowedValue av = allowedValues[i];
            this.allowedValues.add(av.getString());
        }

    }

    public void applyVisitor(IVariantVisitor visitor)
    {
        visitor.visit(this);
    }

    @Override
    public boolean isCompatible(ARTResource cls)
    {
        return cls.getURI().equals(XmlSchema.STRING);
    }

    @Override
    public boolean isLexicalRepresentation(String lexicalForm)
    {
    	// Checks that the given string is equal to one of the allowed values.
        return allowedValues.contains(lexicalForm);
    }

    @Override
    public boolean setValue(String lexicalForm)
    {
        if(isLexicalRepresentation(lexicalForm)) {
            value = lexicalForm;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public String getLexicalRepresentation()
    {
        return value;
    }

	/**
	 * Gets the underlying value.
	 * 
	 * @return the underlying value
	 */
    public String getValue()
    {
        return value;
    }

    @Override
    public String getValueAsString()
    {
        return value;
    }

    /** The underlying value. */
    private String value;
    
    /** The allowed values. */
    private HashSet<String> allowedValues;
}
